package com.android.pantryshopping;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.pantryshopping.adapter.CommodityAdapter;
import com.android.pantryshopping.bean.Commodity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment implements CommodityAdapter.onCommodityItemListener{

    private CommoditySqLiteHelper mCommoditySqLiteHelper;

    private CommodityAdapter mCommodityAdapter;
    private RecyclerView mRecyclerView;
    private FloatingActionButton mFloatingActionButton;

    private ArrayList<Commodity> mCommodities;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (mCommodityAdapter != null && mCommoditySqLiteHelper != null) {
            mCommodities.clear();
            mCommodities.addAll(mCommoditySqLiteHelper.queryAllCommodityData());
            mCommodityAdapter.notifyDataSetChanged();
        }
    }

    private void initListener() {
        mFloatingActionButton.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(),AddActivity.class);
            startActivity(intent);
        });
    }

    private void initData() {
        mCommoditySqLiteHelper = new CommoditySqLiteHelper(requireContext(),"Commodity_db",null,1);
        mCommodities = new ArrayList<>();

        List<Commodity> commodities = mCommoditySqLiteHelper.queryAllCommodityData();
        mCommodities.addAll(commodities);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mCommodityAdapter = new CommodityAdapter(requireContext());
        mCommodityAdapter.setOnCommodityItemListener(this);
        mCommodityAdapter.setCommodities(mCommodities);
        mRecyclerView.setAdapter(mCommodityAdapter);
    }

    private void initView(View root) {
        mRecyclerView = root.findViewById(R.id.recyclerview);
        mFloatingActionButton = root.findViewById(R.id.flo_add);
    }

    @Override
    public void onCommodityClick(int position) {
        Intent intent = new Intent(requireContext(),DetailsActivity.class);
        intent.putExtra("position",position);
        startActivity(intent);
    }
}
